package com.yisu.auction.api.web.controller;

import com.yisu.auction.api.core.service.ISecKillActivityService;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.activityPackage.SecKillActivityRequest;
import com.yisu.auction.api.domain.vo.activityPackageVos.SecKillVo;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.utils.DateUtil;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：秒杀业务
 * createTime：2017/8/24
 */
@Controller
@RequestMapping(value = "/secKill")
public class SecKillActivityController extends BaseController {

    @Resource
    private ISecKillActivityService iSecKillActivityService;

    /**
     * author: 赵杰
     * Business Description：获取当日的秒杀商品列表
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/getCurrentDaySecKillList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult getCurrentDaySecKillList() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSSSS");
            List<SecKillVo> secKillVos = iSecKillActivityService.getCurrentDaySecKillList();
            if (!CollectionUtils.isEmpty(secKillVos)) {
                for (SecKillVo item : secKillVos) {
                    item.setAllowPlaceAnOrder(0);
                    item.setAllowBuyQualification(0);
                    if ((simpleDateFormat.parse(item.getStartTime()).getTime() - (new Date()).getTime() < 0) && ((simpleDateFormat.parse(item.getEndTime()).getTime() - (new Date()).getTime() > 0))) {
                        item.setAllowPlaceAnOrder(1);
                    }
                    if (((simpleDateFormat.parse(item.getStartTime()).getTime() - (new Date()).getTime() > 00) && (DateUtil.diffValue(simpleDateFormat.parse(item.getStartTime()), new Date())/1000) <= 1800)) {
                        item.setAllowBuyQualification(1);
                    }
                }
            }
            return new JsonResult(secKillVos);
        } catch (Exception e) {
            logger.error("获取首页相关业务信息异常：" + e.getMessage());
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }


    /**
     * author: 赵杰
     * Business Description：获取当日的秒杀商品列表
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/secKillAction", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult secKillAction(SecKillActivityRequest secKillActivityRequest) throws InterruptedException {
        this.userToken = secKillActivityRequest.getUserToken();
        secKillActivityRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        return   iSecKillActivityService.secKillAction(secKillActivityRequest);
    }


    /**
     * author: 赵杰
     * Business Description：购买资格
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/buyQualification", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult buyQualification(SecKillActivityRequest secKillActivityRequest) throws Exception {
        this.userToken = secKillActivityRequest.getUserToken();
        secKillActivityRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        return iSecKillActivityService.buyQualification(secKillActivityRequest);
    }


}
